﻿@model Bricks.Business.Models.Account.MenuInfo

@{
    ViewBag.Title = "菜单管理";
    Layout = "~/Views/Shared/_UserPage.cshtml";
}

@*实现对EasyUI的DataGird控件操作的JS代码*@
<script type="text/javascript">
    $(function () {
        //当页面首次刷新的时候执行的事件
        initTable();
    });
    //实现对DataGird控件的绑定操作

    function initTable(queryData) {
        $('#myTreeGrid').treegrid({   //定位到Table标签，Table标签的ID是myTreeGrid
            //   MenuInfo是控制器，GetData是控制器中获取用户数据的一个Action
            url: '/MenuInfo/GetDataList',   //指向后台的Action来获取当前用户的信息的Json格式的数据
            //title: '菜单管理',  //标识
            //width: 'auto',
            //height: 500,
            nowrap: true,
            autoRowHeight: false,
            striped: true,
            collapsible: true,
            rownumbers: true,
            //sortName: 'ID',    //根据某个字段给easyUI排序
            //sortOrder: 'asc',
            remoteSort: false,
            idField: 'ID',
            treeField: 'MenuName',
            parentField: 'PID',
            checkOnSelect: true,
            selectOnCheck: true,
            singleSelect: true,
            queryParams: queryData,  //异步查询的参数
            toolbar: '#myToolBar',
            columns: [[
                { title: '@Html.LabelFor(model => model.MenuName)', field: 'MenuName', width: 200, sortable: true },
                { title: '@Html.LabelFor(model => model.MenuCode)', field: 'MenuCode', width: 150, sortable: true },
                { title: '@Html.LabelFor(model => model.Url)', field: 'Url', width: 220, sortable: true },
                { title: '@Html.LabelFor(model => model.MenuTypeNum)', field: 'MenuTypeString', width: 50, sortable: true },
                {
                    title: '@Html.LabelFor(model => model.Modal)', field: 'Modal', width: 40, sortable: true, align: "center",
                    formatter: function (value, row, index) {
                        return $.GetYesOrNo(value);  //是否模态图标（是/否）
                    }
                },
                { title: '@Html.LabelFor(model => model.Width)', field: 'Width', width: 60, sortable: true },
                { title: '@Html.LabelFor(model => model.Height)', field: 'Height', width: 60, sortable: true },
                {
                    title: '@Html.LabelFor(model => model.IsDeleted)', field: 'IsDeleted', width: 60, sortable: true, align: "center",
                    formatter: function (value, row, index) {
                        return $.GetYesOrNo(value);  //是否禁用图标（是/否）
                    }
                },
                { title: '@Html.LabelFor(model => model.Description)', field: 'Description', width: 220, sortable: true }
            ]]
        });
    }
    var MenuInfo = {
        //新增
        Add: function () {
            var row = $("#myTreeGrid").treegrid("getSelected");
            if (row) {
                MenuInfo.Mutual("新增", "/MenuInfo/Edit?PID=" + row.ID);
            }
            else { MenuInfo.Mutual("新增", "/MenuInfo/Edit"); }
        },

        //修改
        Modify: function () {
            var row = $("#myTreeGrid").treegrid("getSelected");
            if (row) {
                MenuInfo.Mutual("修改", "/MenuInfo/Edit?ID=" + row.ID);
            }
            else { $.EasyuiSlide("请选择一条记录"); }
        },

        //实体操作
        Mutual: function (title, href) {
            $.ModelDialog({
                title: title, href: href, width: 320,
                handler: function () {
                    $.ModelForm({
                        validate: true, button: $(this),
                        id: "#form-model",
                        url: "/MenuInfo/Edit",
                        success: function (result) { MenuInfo.Reload(); }
                    });
                },
                onLoad: function () { MenuInfo.FormInit(); }
            });
        },

        //弹窗初始化
        FormInit: function () {
            //定义父级的机构树
            $("#PID").combotree({
                width: 155, url: "/MenuInfo/GetMenuComboTree"
                //onLoadSuccess: function (node, data) {
                //    var nodeid = $("#ID").val();
                //    if (nodeid) {
                //        var etree = $("#PID").combotree("tree");
                //        etree.tree("remove", etree.tree('find', nodeid).target);
                //    }
                //}
            });

            //定义菜单类型枚举下拉
            $("#MenuTypeNum").combobox({
                editable: false, width: 155, panelHeight: "auto",
                data: [
                    { value: "0", text: "NULL" },
                    { value: "1", text: "标签" },
                    { value: "2", text: "弹窗" },
                    { value: "3", text: "内嵌" }
                ]
            });

            //是否模态
            $("#Modal").combobox({
                editable: false, width: 155, panelHeight: "auto",
                data: [
                    { value: "NULL", text: "NULL" },
                    { value: 1, text: "是" },
                    { value: 0, text: "否" }
                ]
            });
        },

        //Enable: function () {
        //    MenuInfo.EnableMethod(true);
        //},

        //Disable: function () {
        //    MenuInfo.EnableMethod(false);
        //},

        ////禁用或启用
        //EnableMethod: function (isUsed) {
        //    var arr = $('#myTreeGrid').datagrid('getSelections');

        //    if (arr.length == 1) {
        //        //window.location.href = "/MenuInfo/Modify/" + arr[0].Id;
        //        //window.location.href = "/MenuInfo/Used?ID=" + arr[0].ID + "&IsUsed=" + isUsed;
        //        $.post("/MenuInfo/Used?ID=" + arr[0].ID + "&IsUsed=" + isUsed);
        //        $.messager.alert('操作提示', '操作成功', 'info');
        //        $("#myTreeGrid").datagrid("reload");
        //    } else {
        //        $.messager.alert('操作提示', '请选择一条数据!', 'warning');
        //    }
        //    return false;
        //},

        //启用
        Enable: function () {
            MenuInfo.SetEnable("True");
        },

        //禁用
        Disable: function () {
            MenuInfo.SetEnable("False");
        },

        //设置启用或禁用
        SetEnable: function (isEnable) {
            var row = $("#myTreeGrid").treegrid("getSelected");
            if (row) {
                $.post("/MenuInfo/SetEnable", { id: row.ID, isEnable: isEnable }, function (result) {
                    if (result.ResultType == 0) {
                        $.EasyuiSlide(result.Message);
                        MenuInfo.Reload();
                    }
                    else {
                        $.EasyuiMsg(result.Message);
                    }
                });
            }
            else { $.EasyuiSlide("请选择一条记录"); }
        },

        //删除
        Delete: function () {
            var arr = $('#myTreeGrid').datagrid('getSelections');

            if (arr.length == 1) {
                $.messager.confirm('操作提示', "确认删除[ " + arr[0].MenuName + " ]吗？", function (r) {
                    if (r) {
                        $.post("/MenuInfo/Delete?ID=" + arr[0].ID, { query: arr.join(",") }, function (result) {
                            if (result.ResultType == 0) {  //0表示成功
                                //移除删除的数据
                                //$("#myTreeGrid").datagrid("reload");
                                //$("#myTreeGrid").datagrid("clearSelections");
                                //$.messager.alert('操作提示', '删除成功!', 'info');
                                $.EasyuiSlide(result.Message);
                                MenuInfo.Reload();
                            }
                            else {
                                $.EasyuiMsg(result.Message);
                            }
                        });
                    }
                });

            } else {
                //$.messager.alert('操作提示', '请选择一条数据!', 'warning');
                $.EasyuiMsg("请选择一条数据");
            }
            return false;
        },

        //向上
        Up: function () {
            MenuInfo.Order("Up");
        },

        //向下
        Down: function () {
            MenuInfo.Order("Down");
        },

        //向上或向下方法
        Order: function (order) {
            var row = $("#myTreeGrid").treegrid("getSelected");
            if (row) {
                //id：识别当前记录；pid：识别涉及调整的记录集合；order：向上或向下
                $.post("/MenuInfo/SetOrder", { id: row.ID, pid: row.PID, order: order }, function (result) {
                    if (result.ResultType == 0) {
                        $.EasyuiSlide(result.Message);  //成功则右下角提示
                        MenuInfo.Reload();
                        //$("#myTreeGrid").treegrid("selectRecord", row.ID);  //重新选中对应ID的记录（此处不生效，原因不明，后续探究）
                    }
                    else {
                        $.EasyuiMsg(result.Message);  //失败则弹出框提示
                    }
                });
            }
            else { $.EasyuiSlide(nullrowmsg); }
        },

        //分配功能
        Assign: function () {
            var row = $("#myTreeGrid").treegrid("getSelected");
            if (row) {
                MenuInfo.AssignMutual("分配功能", "/MenuInfo/Assign?ID=" + row.ID, row.ID);
            }
            else {
                $.EasyuiMsg("请选择一条数据");
            }
        },

        //分配实体操作
        AssignMutual: function (title, href, menuID) {
            MenuInfo.ModelDialogAssign({
                title: title, href: href, width: 765, menuID: menuID,
                //handler: function () {
                //    var data = $('#actionDataGrid').datagrid('getData');
                //    var dataJson = JSON.stringify(data);
                //    var effectRow = new Object();
                //    effectRow["Data"] = dataJson;
                //    $.ModelForm({
                //        validate: true, button: $(this),
                //        id: "#form-menuAction",
                //        url: "/MenuInfo/AssignPost",
                //        //这里还需要传参数effectRow------------------------------------------------------------------
                //        //param:effectRow
                //        //这里还需要传参数effectRow------------------------------------------------------------------
                //        success: function (result) {
                //            //MenuInfo.Reload();
                //            $.EasyuiSlide("分配功能（响应）");
                //        }
                //    });
                //},
                onLoad: function () {
                    //MenuInfo.FormInit();
                }
            });
        },

        //实体窗口（分配）
        ModelDialogAssign: function (options) {
            if (options.dialogid) {
                options.dialogid = "id='" + options.dialogid + "'";
            }
            var buttons = [];
            if (!options.width) { options.width = "auto"; }
            if (!options.height) { options.height = "auto"; }

            buttons.push({
                text: "确定", iconCls: "icon-ok",
                handler: function () {
                    //$.ModelForm({
                    //    validate: true, button: $(this),
                    //    id: "#form-menuAction",
                    //    url: "/Purview/CoverPurviewPost?configType=" + $("#hConfigType").val(),
                    //    success: function (result) {
                    //        //$.EasyuiMsg(result);
                    //        $("#myTreeGrid").treegrid("reload");
                    //    }
                    //});

                    MenuInfo.EndEdit();  //结束编辑

                    var data = $('#actionDataGrid').datagrid('getRows');  //取得所有数据

                    //var data = $('#actionDataGrid').datagrid('getChanges');
                    //var data = $('#actionDataGrid').datagrid('getChanges', "updated");
                    var dataJson = JSON.stringify(data);
                    var effectRow = new Object();
                    effectRow["Data"] = dataJson;

                    $.post("/MenuInfo/AssignPost?menuID=" + options.menuID, effectRow, function (result) {
                        try {
                            result = JSON.parse(result);
                            if (result.ResultType == 0) {  //ResultType是在OperationResultType定义的，0表示Success成功
                                $.EasyuiSlide(result.Message);  //弹出右下角提示
                                //options.success(result);

                                //关闭窗体
                                var button = $("#btnClose");
                                $.ButtonCloseDialog(button);
                            }
                            else {
                                //如果失败，还是使每一行处于编辑状态
                                //var list = $('#actionDataGrid').datagrid('getData').rows;
                                //for (var i = 0; i < list.length; i++) {
                                //    //Assign.EditRow(i);
                                //    $('#actionDataGrid').datagrid('beginEdit', i);
                                //}
                                $.EasyuiMsg(result.Message);  //弹出提示窗体
                            }
                        } catch (e) { $.Error(result); }
                    });
                }
            });
            buttons.push({ id: "btnClose", text: "关闭", iconCls: "icon-no", handler: function () { $.ButtonCloseDialog(this); } });

            var dialog = $("<div " + options.dialogid + " />").dialog({
                title: options.title, href: options.href,
                width: options.width, height: options.height,
                toolbar: options.toolbar, content: options.content,
                modal: true, cache: false, collapsible: true,
                maximizable: options.maximizable,  //允许最大化
                buttons: buttons, iconCls: options.iconCls,
                onLoad: function () {
                    options.onLoad(); $.AdjustmentDialog(dialog);
                },
                onClose: function () { $(this).dialog("destroy"); }
            });
            return dialog;
        },

        //结束编辑
        EndEdit: function () {
            var rows = $('#actionDataGrid').datagrid('getRows');
            for (var i = 0; i < rows.length; i++) {
                $('#actionDataGrid').datagrid('endEdit', i);
            }
        },

        //刷新
        Reload: function () {
            $("#myTreeGrid").treegrid("reload");
        }
    }
</script>
<!--面板（从数据库取得菜单名称和图标等）-->
<div class="easyui-panel" @Html.Raw(ViewBag.PanelInfo)>
    <table id="myTreeGrid" fit="true"></table>
    <!--数据列表（根据JS构造数据）-->
    <div id="myToolBar">@Html.Raw(ViewBag.ToolBarInfo)</div>
    <!--工具栏（从数据库取得菜单菜单的名称和图标等）-->
</div>
